import { AsyncThunk, createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { ActionReducerMapBuilder } from '@reduxjs/toolkit';
import type { BindMobileLoginParams, BindMobileLoginResponse, CodeLoginParams, CodeLoginResponse, PasswordLoginParams, PasswordLoginResponse, QQLoginParams, QQLoginResponse, RegisterParams, SendCodeParams } from '@/type/LoginType';
import { bindMobileLoginAPI, cancelBindMobileAPI, codeLoginAPI, logoutAPI, passwordLoginAPI, qqLoginAPI, registerAPI, sendCodeAPI } from '@/API/loginAPI';
import type { AsyncThunkConfig } from '@reduxjs/toolkit/dist/createAsyncThunk';
import { get, remove, set } from '@/utils/localStorage';


interface InitialState {
  loginInfo: PasswordLoginResponse | CodeLoginResponse | QQLoginResponse | BindMobileLoginResponse;
}

const initialState: InitialState = {
  loginInfo: get() || {} as PasswordLoginResponse | CodeLoginResponse | QQLoginResponse | BindMobileLoginResponse,
};

/**
 *
 * @description 密码登录的异步函数
 * */
export const passwordLogin: AsyncThunk<PasswordLoginResponse, PasswordLoginParams, AsyncThunkConfig> = createAsyncThunk('passwordLogin', async (passwordLoginParams, { rejectWithValue }) => {
  try {
    const { data } = await passwordLoginAPI(passwordLoginParams);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});
/**
 *
 * @description 验证码登录的异步函数
 * */
export const codeLogin: AsyncThunk<CodeLoginResponse, CodeLoginParams, AsyncThunkConfig> = createAsyncThunk('codeLogin', async (codeLoginParams, { rejectWithValue }) => {
  try {
    const { data } = await codeLoginAPI(codeLoginParams);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 *@description qq登录的异步函数
 * */
export const qqLogin: AsyncThunk<QQLoginResponse, QQLoginParams, AsyncThunkConfig> = createAsyncThunk('qqLogin', async (qqLoginParams, { rejectWithValue }) => {
  try {
    const { data } = await qqLoginAPI(qqLoginParams);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 *
 * @description 获取验证码的异步函数
 * */
export const sendCodeThunk = createAsyncThunk('sendCoded', async (params: SendCodeParams, { rejectWithValue }) => {
  try {
    const { data } = await sendCodeAPI(params);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * qq登录失败后绑定手机号进行登录的异步函数
 * */
export const bindMobileLogin: AsyncThunk<BindMobileLoginResponse, BindMobileLoginParams, AsyncThunkConfig> = createAsyncThunk('bindMobileLogin', async (bindMobileParams, { rejectWithValue }) => {
  try {
    const { data } = await bindMobileLoginAPI(bindMobileParams);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * 解除三方绑定的异步函数
 * */
export const cancelBindMobile: AsyncThunk<null, string, AsyncThunkConfig> = createAsyncThunk('cancelBindMobile', async (params, { rejectWithValue }) => {
  try {
    const { data } = await cancelBindMobileAPI(params);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 注册账号的异步函数
 * */
export const registerAccount: AsyncThunk<{ id: string }, RegisterParams, AsyncThunkConfig> = createAsyncThunk('registerAccount', async (registerParams, { rejectWithValue }) => {
  try {
    const { data } = await registerAPI(registerParams);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 退出登录的异步函数
 * */
export const logout = createAsyncThunk('logout', async (_, { rejectWithValue }) => {
  try {
    const { data } = await logoutAPI();
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

const loginStore = createSlice({
  name: 'loginStore',
  initialState,
  reducers: {},
  extraReducers(builder: ActionReducerMapBuilder<InitialState>) {
    builder.addCase(passwordLogin.fulfilled, (state: InitialState, action: PayloadAction<PasswordLoginResponse>) => {
      const { payload } = action;
      state.loginInfo = payload;
      set(payload);
    });
    builder.addCase(codeLogin.fulfilled, (state: InitialState, action: PayloadAction<CodeLoginResponse>) => {
      const { payload } = action;
      state.loginInfo = payload;
      set(payload);
    });
    builder.addCase(qqLogin.fulfilled, (state: InitialState, action: PayloadAction<QQLoginResponse>) => {
      const { payload } = action;
      state.loginInfo = payload;
      set(payload);

    });
    builder.addCase(bindMobileLogin.fulfilled, (state: InitialState, action: PayloadAction<BindMobileLoginResponse>) => {
      const { payload } = action;
      state.loginInfo = payload;
      set(payload);
    });
    builder.addCase(logout.fulfilled, (state: InitialState, action: PayloadAction<null>) => {
      state.loginInfo = {} as PasswordLoginResponse | CodeLoginResponse;
      remove();
    });
  },
});


export default loginStore.reducer;